﻿@{
    Bundle("browserStorage");
    ViewBag.PageHistory = ViewBag.PageHistory ?? false;
    int historySize = Svc.IsAuthenticated ? Svc.Attribute(Par.In("WebPortal").Of("Preferences").Name("HistorySize"), 15) : 15;
}
@if(historySize != 0)
{
    <script type="text/javascript">
        jQuery(document).ready(function() {
            if (!jQuery.browserStorage) {
                jQuery('@ID("#pnl")').hide();
                return;
            }
            var storageKey = '@Par.In("UserStorage").Name("History")';
            var pageHistory = @ViewBag.PageHistory.ToString().ToLowerInvariant();
            var history = jQuery.browserStorage.getItem(storageKey, true);
            var historySize = @historySize;
            if (history == null)
                history = new Array();
            if (pageHistory) {
                var pageTitle = '@ViewBag.PageTitle';
                var pageUrl = '@Request.RawUrl';
                var pageVisited = '@Svc.DateTime(DateTime.UtcNow)';
                var oldIndex = -1;
                jQuery(history).each(function(idx) {
                    if (this.url != pageUrl)
                        return true;
                    oldIndex = idx;
                    return false;
                });
                if (oldIndex != -1)
                    history.splice(oldIndex, 1);
                history.unshift({
                    visited: pageVisited,
                    title: pageTitle,
                    url: pageUrl
                });
            }
            if (history.length > historySize)
                history.splice(historySize);
            jQuery.browserStorage.setItem(storageKey, history, true);
            if (history.length == 0) {
                jQuery('@ID("#pnl")').hide();
            } else {
                jQuery(history).each(function() {
                    jQuery('@ID("#lst")').append('<li class="unit history"><span>' + this.visited + '</span>&nbsp;<a href="' + this.url + '">' + this.title + '</a></li>');
                });
            }
        });
    </script>
    <div class="box" id="@ID("pnl")">
        <header>
            <h3>@T(Res.Name("Title"))</h3>
        </header>
        <section>
            <ul class="unit-list history latest" id="@ID("lst")"></ul>
        </section>
    </div>
}